package org.example;

public class Main2 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
        rearrangeEvenOdd(a);
        System.out.print("a={");
        for (int num : a) {
            System.out.print(num + " ");
        }
        System.out.print("}");
    }

    public static void rearrangeEvenOdd(int[] a) {
        if (a == null || a.length <= 1) {
            return;
        }

        int[] result = new int[a.length];
        int index = 0;

        // Move odd-indexed elements to the front
        for (int i = 1; i < a.length; i += 2) {
            result[index++] = a[i];
        }

        // Move even-indexed elements to the back
        for (int i = 0; i < a.length; i += 2) {
            result[index++] = a[i];
        }

        // Copy the result back to the original array
        System.arraycopy(result, 0, a, 0, a.length);
    }
}